package com.glsc.ngateway.common.base.repo.mysql.flowable;

import com.glsc.ngateway.common.base.domain.mysql.flowable.FlowableDetail;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @author lium
 * @date 2021/8/25
 */
public interface FlowableDetailRepo extends JpaRepository<FlowableDetail, Long>, JpaSpecificationExecutor<FlowableDetail> {

    @Query(value = "select m.content from FlowableDetail m where m.taskId = :taskId")
    String queryFormData(@Param("taskId") String taskId);

    @Query(value = "select m.content from FlowableDetail m where m.processInstanceId = :processId and m.taskName = 'start'")
    String queryStartDetailByProcessId(@Param("processId") String processId);

    @Query(value = "select m from FlowableDetail m where m.processInstanceId = :processId order by m.opTime desc")
    List<FlowableDetail> queryDetailByProcessId(@Param("processId") String processId);

    @Query(value = "select m from FlowableDetail m where m.processStatus = :status")
    List<FlowableDetail> findAllByProcessStatus(@Param("status") String s);

    @Query(value = "select m from FlowableDetail m " +
            "where 1=1 " +
            "and (coalesce(:processIds, null) is null or m.processInstanceId  in (:processIds)) " +
            "and (:statusNot is null or (m.processStatus != :statusNot or m.processStatus is null )) " +
            ""
    )
    List<FlowableDetail> findAllByProcessIdIn(@Param("processIds") List<String> processIds, @Param("statusNot") String statusNot);
}
